package cn.anosi.service.auth;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Range;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.github.wenhao.jpa.Specifications;

import cn.anosi.dto.OrganizationDto;
import cn.anosi.entity.auth.Organization;
import cn.anosi.page.PageResponseBean;
import cn.anosi.page.Pager;
import cn.anosi.repository.auth.OrganizationRepository;
import cn.anosi.service.base.impl.BaseServiceImpl;
import cn.anosi.utils.ObjectsUtils;
import cn.anosi.utils.PageUtils;
import cn.anosi.vo.OrganizationTree;

@Service
public class OrganizationService extends BaseServiceImpl<Organization>{

	@Autowired
	public OrganizationService(OrganizationRepository organizationRepository) {
		super(organizationRepository);
	}

	@Autowired
	protected OrganizationRepository organizationRepository;
	

	/**
	 * 组织机构树
	 * @return
	 */
	public List<OrganizationTree> tree() {
		return organizationRepository.tree();
	}

	/**
	 * 带查询的列表
	 * @param pager
	 * @param pId
	 * @param dto
	 * @return
	 */
	public PageResponseBean<Organization> list(Pager pager, Integer pId, OrganizationDto dto) {
		Specification<Organization> specification = Specifications.<Organization>and()
				.eq("isDel", 0)
	            .eq("parent.id", pId)
	            .like(StringUtils.isNotBlank(dto.getName()), "name", "%" + dto.getName() +"%")
	            .between(ObjectsUtils.notNull(dto.getStartTime(), dto.getEndTime()), "createTime", new Range<>(dto.getStartTime(), dto.getEndTime()))
	            .build();
		
		Pageable pageable = new PageRequest(pager.getPageNum(), pager.getPageSize(), pager.getDirection(), pager.getProperty());
		Page<Organization> datas = organizationRepository.findAll(specification, pageable);
		
		return PageUtils.getPageResponseBean(datas, pager);
	}

	/**
	 * 根据名称查询机构
	 * @param name
	 * @return
	 */
	public Organization findByName(String name) {
		return organizationRepository.findByNameAndIsDel(name, 0);
	}

	/**
	 * 除当前机构以外的其他机构
	 * @param id
	 * @return
	 */
	public List<Organization> others(Integer id) {
		return organizationRepository.findByIdIsNot(id);
	}

	/**
	 * 查询列表
	 * @param pId
	 * @param dto
	 * @return
	 */
	public List<Organization> list(Integer pId, OrganizationDto dto) {
		Specification<Organization> specification = Specifications.<Organization>and()
				.eq("isDel", 0)
	            .eq("parent.id", pId)
	            .like(StringUtils.isNotBlank(dto.getName()), "name", "%" + dto.getName() +"%")
	            .between(ObjectsUtils.notNull(dto.getStartTime(), dto.getEndTime()), "createTime", new Range<>(dto.getStartTime(), dto.getEndTime()))
	            .build();
		
		List<Organization> datas = organizationRepository.findAll(specification);
		
		return datas;
	}
}
